import { request } from './request';

/**
 * 获取家族树（以当前登录用户为中心的5代成员）
 */
export function getFamilyTree() {
  return request({
    url: '/family/tree/miniapp',
    method: 'GET'
  });
}

/**
 * 获取增强版家族树（包含配偶和同代成员）
 * @param {Object} params - 请求参数
 * @param {Number} params.memberId - 中心成员ID，不传则使用当前登录用户绑定的成员
 * @param {Number} params.generations - 显示的代数，默认5代
 * @param {Boolean} params.includeSiblings - 是否包含同代成员，默认true
 */
export function getEnhancedFamilyTree(params = {}) {
  // 如果后端接口不存在，提供模拟数据用于前端开发
  const useMockData = true; // 设置为false使用真实接口

  if (useMockData) {
    return new Promise((resolve) => {
      // 模拟延迟
      setTimeout(() => {
        resolve({
          code: 0,
          msg: "操作成功",
          data: {
            totalMembers: 6,
            success: true,
            centerMemberId: 4,
            totalGenerations: 8,
            tree: [
              {
                generation: 1,
                gender: "M",
                branch_id: 2,
                children: [
                  {
                    generation: 2,
                    mother_id: 2,
                    gender: "M",
                    branch_id: 2,
                    // 添加兄弟姐妹
                    siblings: [
                      {
                        id: 5,
                        name: "陈大伯",
                        gender: "M",
                        generation: 2,
                        birth_date: "2025-05-20",
                        photo: "/member-photos/2025/07/23/sibling1.jpg",
                        father_id: 1
                      },
                      {
                        id: 6,
                        name: "陈小姑",
                        gender: "F",
                        generation: 2,
                        birth_date: "2025-07-15",
                        photo: "/member-photos/2025/07/23/sibling2.jpg",
                        father_id: 1
                      }
                    ],
                    children: [
                      {
                        generation: 3,
                        gender: "M",
                        branch_id: 2,
                        birth_date: "2025-06-30",
                        contact: "1324234325",
                        name: "陈志翔",
                        photo: "/member-photos/2025/07/23/1cbc2155-e525-4d93-acaf-b4d15a4c06cf.jpg",
                        id: 4,
                        father_id: 3,
                        story: "地方功夫帝皇高鸿股份解决",
                        // 添加配偶
                        spouse_id: 7,
                        spouse_name: "李小花",
                        spouse_gender: "F",
                        spouse_birth_date: "2025-08-12",
                        spouse_photo: "/member-photos/2025/07/23/spouse1.jpg"
                      }
                    ],
                    birth_date: "2025-06-30",
                    contact: "12375435345",
                    name: "陈安邦",
                    photo: "/member-photos/2025/07/23/048aec4e-8873-495b-9ecf-45ffa9964154.jpg",
                    id: 3,
                    father_id: 1,
                    story: "3432贵妇膏就是循序",
                    // 添加配偶
                    spouse_id: 2,
                    spouse_name: "王氏",
                    spouse_gender: "F",
                    spouse_birth_date: "2025-06-25",
                    spouse_photo: "/member-photos/2025/07/23/spouse2.jpg"
                  }
                ],
                birth_date: "2025-06-28",
                contact: "4332",
                name: "张成正",
                photo: "/member-photos/2025/07/23/c4d0b088-4188-4ca2-ad36-19b6027b03d8.jpg",
                id: 1,
                death_date: "2025-06-30",
                story: "国际饭店回访电话大汉帝国发的"
              }
            ]
          }
        });
      }, 500);
    });
  }

  return request({
    url: '/api/v1/family/tree/enhanced',
    method: 'GET',
    data: {
      memberId: params.memberId,
      generations: params.generations || 5,
      includeSiblings: params.includeSiblings !== false
    }
  });
}

/**
 * 获取成员的配偶信息
 * @param {Number} memberId - 成员ID
 */
export function getMemberSpouses(memberId) {
  return request({
    url: `/api/v1/members/${memberId}/spouses`,
    method: 'GET'
  });
}

/**
 * 获取成员的兄弟姐妹（同父或同母的成员）
 * @param {Number} memberId - 成员ID
 */
export function getMemberSiblings(memberId) {
  return request({
    url: `/api/v1/members/${memberId}/siblings`,
    method: 'GET'
  });
}

/**
 * 搜索成员并获取以该成员为中心的家族树
 * @param {Object} params - 搜索参数
 * @param {String} params.name - 成员姓名关键词
 */
export function searchFamilyTree(params) {
  return request({
    url: '/api/v1/family/tree/search',
    method: 'GET',
    data: params
  });
}

/**
 * 添加子节点（需要审核）
 * @param {Object} data - 子节点数据
 * @param {Number} data.parentId - 父节点ID
 * @param {String} data.name - 姓名
 * @param {String} data.gender - 性别(M/F)
 * @param {String} data.birthDate - 出生日期(可选)
 * @param {String} data.deathDate - 去世日期(可选)
 * @param {String} data.photo - 照片(可选)
 * @param {String} data.story - 个人事迹(可选)
 * @param {String} data.nativePlace - 籍贯(可选)
 * @param {String} data.currentAddress - 现住地址(可选)
 */
export function addChild(data) {
  return request({
    url: '/api/v1/family/tree/add-child',
    method: 'POST',
    data: data
  });
}

/**
 * 添加配偶（需要审核）
 * @param {Object} data - 配偶节点数据
 * @param {Number} data.memberId - 成员ID
 * @param {String} data.name - 姓名
 * @param {String} data.gender - 性别(M/F)
 * @param {String} data.birthDate - 出生日期(可选)
 * @param {String} data.deathDate - 去世日期(可选)
 * @param {String} data.photo - 照片(可选)
 * @param {String} data.story - 个人事迹(可选)
 * @param {String} data.nativePlace - 籍贯(可选)
 * @param {String} data.currentAddress - 现住地址(可选)
 */
export function addSpouse(data) {
  return request({
    url: '/api/v1/family/tree/add-spouse',
    method: 'POST',
    data: data
  });
}

/**
 * 获取成员详情
 * @param {Number} id - 成员ID
 */
export function getMemberDetail(id) {
  return request({
    url: `/api/v1/members/${id}`,
    method: 'GET'
  });
} 